package com.dj.uphome.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.dj.common.web.controller.CoreCrudRestController;
import com.dj.uphome.web.service.system.SysMenuService;
import com.dj.uphome.web.service.system.SysRoleRightsService;
import com.dj.uphome.web.vo.system.SysMenuVo;
import com.dj.uphome.web.vo.system.SysRoleUserVo;

/**
 * 类名称：角色管理
 */
@Controller
@RequestMapping(value = "/role/rights")
public class RoleRightsController extends CoreCrudRestController{
	@Autowired
	private SysRoleRightsService roleRightsService;
	@Autowired
	private SysMenuService menuService;

	
	@RequestMapping(value = "/getRights", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
	public  ResponseEntity<List<SysMenuVo>> getRights(@RequestParam int roleId) throws Exception {
		List<SysMenuVo> list = menuService.findListByRoleId(roleId);
		return new ResponseEntity<>(list,HttpStatus.OK);
	}
	/**
	 * 保存角色菜单权限
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE })
	public ResponseEntity<SysRoleUserVo> save(@RequestParam int roleId, @RequestParam String menuIds)
			throws Exception {
		if (!StringUtils.isEmpty(menuIds)) {
			String[] menuIds_ = menuIds.split(",");
			int[] menuIds_int = new int[menuIds_.length];
			for (int i = 0; i < menuIds_.length; i++) {
				menuIds_int[i] = Integer.parseInt(menuIds_[i]);
			}
			roleRightsService.create(roleId, menuIds_int);
		}
		return new ResponseEntity<>(HttpStatus.OK);
	}

}
